package cn.wolfcode.tieba.controller;

import cn.wolfcode.tieba.annotation.RequireLogin;
import cn.wolfcode.tieba.annotation.UserParam;
import cn.wolfcode.tieba.domain.Comment;
import cn.wolfcode.tieba.domain.User;
import cn.wolfcode.tieba.service.ICommentService;
import cn.wolfcode.tieba.service.ITieziService;
import cn.wolfcode.tieba.util.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王金龙
 * @date 2021/2/6 23:19
 */
@RestController
@RequestMapping("p")
public class CommentController {
    @Autowired
    private ICommentService commentService;
    @Autowired
    private ITieziService tieziService;

    @GetMapping
    public Object list(Long id) {
        return JsonResult.success(commentService.listByTieziId(id));
    }

    @GetMapping("/title")
    public Object title(Long id) {
        return JsonResult.success(tieziService.getById(id));
    }

    @PostMapping("/saveComment")
    @RequireLogin
    public Object saveComment(Long id, String content, @UserParam User user) {
        commentService.saveComment(id, content, user);
        return JsonResult.success();
    }

    @PostMapping("/saveComments")
    @RequireLogin
    public Object saveComments(Long id, Long commentId, String content, @UserParam User user) {
        commentService.saveComments(id,commentId, content, user);
        return JsonResult.success();
    }

    @GetMapping("/replyNum")
    public Object replyNum(Long id) {
        return JsonResult.success(commentService.count(new QueryWrapper<Comment>().eq("tiezi_id",id)) - 1);
    }
}
